#!/user/bin/even Python3
# -*- coding:utf-8 -*-
# HistorySave.py
# 历史数据留存
# author:zhaohexin
# time：2020/7/29 14:46
import configparser
import os

from configobj import ConfigObj

from conf import SendHistory


class SaveHistory:
    def __init__(self):
        self.path = SendHistory.temp_path
        self.search_url = SendHistory.search_url
        self.search_industry = SendHistory.search_industry
        self.search_career = SendHistory.search_career
        self.excel_path = SendHistory.excel_path

    def save(self):
        """
        历史数据保存
        :return:
        """
        # 创建临时文件夹
        try:
            os.makedirs(os.path.dirname(self.path))
        except:
            pass
        # 判断文件是否存在，若不存在，创建写入当前值；若存在，判断当前值是否存在，若不存在加入
        if os.path.exists(self.path):
            history = ConfigObj(self.path, encoding="utf-8")
            # 读取历史文件
            history_datas = self.get_history_data()
            # 比对/存数
            history_datas["url_history"].append(self.search_url) if self.search_url not in history_datas[
                "url_history"] and self.search_url != "" else history_datas["url_history"]
            history_datas["industry_history"].append(self.search_industry) if self.search_industry not in \
                                                                              history_datas[
                                                                                  "industry_history"] and self.search_industry != "" else \
                history_datas["industry_history"]
            history_datas["career_history"].append(self.search_career) if self.search_career not in \
                                                                          history_datas[
                                                                              "career_history"] and self.search_career != "" else \
                history_datas["career_history"]
            history_datas["excel_history"].append(self.excel_path) if self.excel_path not in \
                                                                      history_datas[
                                                                          "excel_history"] and self.excel_path != "" else \
                history_datas["excel_history"]
            history["SEARCH"]["search_url"] = history_datas["url_history"]
            history["SEARCH"]["search_industry"] = history_datas["industry_history"]
            history["SEARCH"]["search_career"] = history_datas["career_history"]
            history["SEARCH"]["excel_path"] = history_datas["excel_history"]

            history.write()
        else:
            history = ConfigObj(self.path, encoding="utf-8")
            history["SEARCH"] = {}
            history["SEARCH"]["search_url"] = [self.search_url]
            history["SEARCH"]["search_industry"] = [self.search_industry]
            history["SEARCH"]["search_career"] = [self.search_career]
            history["SEARCH"]["excel_path"] = [self.excel_path]
            history.write()
        return history

    def get_history_data(self):
        """
        获取历史数据
        :return:
        """
        history_data = configparser.ConfigParser()
        history_data.read(self.path, encoding="utf-8")
        url_history = list(set([i.strip() for i in
                                (list(filter(None,
                                             history_data.get("SEARCH", "search_url").split(
                                                 ","))) if history_data.get(
                                    "SEARCH",
                                    "search_url") != '""' else []
                                 )]))
        industry_history = list(set([i.strip() for i in
                                     (list(filter(None,
                                                  history_data.get("SEARCH", "search_industry").split(
                                                      ","))) if history_data.get(
                                         "SEARCH",
                                         "search_industry") != '""' else []
                                      )]))
        career_history = list(set([i.strip() for i in
                                   (list(filter(None,
                                                history_data.get("SEARCH", "search_career").split(
                                                    ","))) if history_data.get(
                                       "SEARCH",
                                       "search_career") != '""' else []
                                    )]))
        excel_history = list(set([i.strip() for i in
                                  (list(filter(None,
                                               history_data.get("SEARCH", "excel_path").split(
                                                   ","))) if history_data.get(
                                      "SEARCH",
                                      "excel_path") != '""' else []
                                   )]))

        return {
            "url_history": url_history,
            "industry_history": industry_history,
            "career_history": career_history,
            "excel_history": excel_history,
        }


if __name__ == '__main__':
    SaveHistory().save()
